<?php
/**
 * Controller is the customized base controller class.
 * All controller classes for this application should extend from this base class.
 */
 Yii::import('dao.share.class.InviteEmail');
class Controller extends CController
{
	/**
	 * @var string the default layout for the controller view. Defaults to '//layouts/column1',
	 * meaning using a single column layout. See 'protected/views/layouts/column1.php'.
	 */
	public $layout='//layouts/default';
	/**
	 * @var array context menu items. This property will be assigned to {@link CMenu::items}.
	 */
	public $menu=array();
	/**
	 * @var array the breadcrumbs of the current page. The value of this property will
	 * be assigned to {@link CBreadcrumbs::links}. Please refer to {@link CBreadcrumbs::links}
	 * for more details on how to specify this property.
	 */
	public $breadcrumbs=array();
	public $category_list;
	public $category_category_relations;
	public $category;
	public $customer_id;
	public $cache_id;
	public $bids;
	public $inviteEmail=null;
	
	public function init(){
	   $this->pageTitle='eGunner Penny Auctions -  Buy Guns and Accessories at Penny Auction Prices.';
	   if ($this->inviteEmail===null){
    	   $this->inviteEmail=new InviteEmail('visit');
	   }
	   $this->inviteEmail->refer=!empty($_GET['ref'])?$_GET['ref']:null;
	   if ($this->inviteEmail->refer!==null && $this->inviteEmail->validate()){
	       $this->inviteEmail->visit();
	   }
	   
	   Yii::app()->clientScript->registerCoreScript('jquery'); 
	   $this->category_list=Category::model()->findAll();
	   $this->category_category_relations=CategoryCategoryRelations::model()->findAllByAttributes(array('parent_category_id'=>'0'));

	   /**
	    * 同步登出
	    */
	   if(!empty($_GET['auto_logout'])){
	       if($_GET['auto_logout']==1){
	           Yii::app()->clientScript->registerScript('auto_logout',"
                    $.ajax({
                       url:'".Yii::app()->params['egunner']."palogout.php',
                                dataType:'jsonp',
                                data:'',
                                jsonp:'callback',
                                success:function(result) {
                                    console.log(result);
                        },
                   });
               ");
	           $_SESSION['auto_logout']=0;
	       }
	   }
	   
	   $customer_id=Yii::app()->user->id;
	   $cache_id=Yii::app()->dbRedis->get('LOGOUT_'.$customer_id);
	   if($cache_id && empty($_SESSION['is_admin'])){
	       Yii::app()->user->logout(true);
	   }
	   
	   if(!empty($_SESSION['auto_login'])){
	       if($_SESSION['auto_login']==1){
	           $customer_info=Customer::model()->findByPk(Yii::app()->user->id);
	           Yii::app()->clientScript->registerScript('auto_login',"
	           $.ajax({
	           url:'".Yii::app()->params['egunner']."palogin.php?email=$customer_info->email',
	           dataType:'jsonp',
	           data:'',
	           jsonp:'callback',
	           success:function(result) {
	           console.log(result);
	       },
	       });
	   
	       ");
	           $_SESSION['auto_login']=0;
	       }
	   }
	   
	   
	   if(!Yii::app()->user->isGuest){
	       $this->bids=CustomerBidInfo::model()->findAll('customer_id='.Yii::app()->user->id);
	   }
		
	   /**
	    * 如果登录状态，插入登录表中数据
	    */
	   if (!Yii::app()->user->isGuest){
	       if (!session_id()){
    	       session_start();
	       }
	       $sid=session_id();
	       $userLogin=new UserLogin();
	       $userLogin=$userLogin->findByAttributes([
	           "user_id"=>Yii::app()->user->id
	       ]);
	       if (empty($userLogin)){
	           $userLogin=new UserLogin();
	       }
	       $userLogin->attributes=[
	           "sid"=>$sid,"created"=>time(),"user_id"=>Yii::app()->user->id,
	           "ip"=>Yii::app()->tools->getIp(),
	       ];
	       $userLogin->save();
	   }
	}
	public function https(){
	
	    $return = false;
	    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])){
	        $serverHeader = $_SERVER['HTTP_X_FORWARDED_PROTO'];
	        if($serverHeader != 'https'){
	            $return = true;
	        }
	        	
	    }else{
	        if($_SERVER['SERVER_PORT'] != '443'){
	            $return = true;
	        }
	    }
	
	    if ($return){
	        Yii::app()->getRequest()->redirect('https://'.$_SERVER['HTTP_HOST'].Yii::app()->request->getUrl());
	    }
	
	}
	
	public function http($http = null){
	    $return = false;
	    if(isset($_SERVER['HTTP_X_FORWARDED_PROTO'])){
	        $serverHeader = $_SERVER['HTTP_X_FORWARDED_PROTO'];
	        if($serverHeader != 'http'){
	            $return = true;
	        }
	        	
	    }else{
	        if($_SERVER['SERVER_PORT'] == '443'){
	            $return = true;
	        }
	    }
	
	    if ($return){
	        Yii::app()->getRequest()->redirect('http://'.$_SERVER['HTTP_HOST'].Yii::app()->request->getUrl());
	    }
	
	}		
	public function filters(){
	    return array(
	        'AccessControl',
	         array(
                'application.filters.LoginFilters',
	        ),
	    );
 	}
}